<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="titleType" label="类型" width="150">
        <template slot-scope="scope">
          {{ questiontype(scope.row.titleType) }}
        </template>
      </el-table-column>
      <el-table-column prop="question" label="题目" width="250">
      </el-table-column>
      <el-table-column prop="answer" label="答案" width="292">
      </el-table-column>
      <el-table-column prop="iserror" label="是否答错" width="120">
        <!-- {{ questiontype(scope.row.iserror) }} -->
       <!-- <template>
     
    </template> -->
      </el-table-column>
      <el-table-column label="批改" width="120" align="center">
        <template slot-scope="scope">
          <!-- <el-button @click="handleClick(scope.row)" type="text"
            >查看</el-button
          > -->
          <el-button type="success" icon="el-icon-check" @click="startcorrect(scope.row,scope.$index)" circle></el-button>
          <el-button type="danger" icon="el-icon-close" @click="iserror(scope.row,scope.$index)" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-button type="primary" @click="submit">完成批改</el-button>
  </div>
</template>
<script>
import qs from 'qs';
export default {
   data() {
    return {
        radio: "",
       tableData: [],
      type_question: ["选择题", "填空题", "解答题", "作文题"],
      titlecorr:{},
      correctTitle:{},
    
    
      // sid: this.$route.params.sid
       sid: this.$store.state.conpletedid,
       lid:[1,2,3]
       //
    };
  },
  methods: { //<el-input>框会出现无法输入消息的情况,这个方法绑定了@input,强制刷新
  //批改完成，提交
  submit(){
      console.log(this.tableData);
     this.correctTitle=this.tableData
     this.correctTitle[0].u_id=this.$store.state.userid
      //    this.$axios
      //   .post(
      //     "http://localhost:8080/correcting_homework",this.correctTitle,
      //     { headers: { "Content-Type": "application/JSON" }}
      //     //完成作业ID
      //   // {params:{list: this.tableData}}
      //   )
      //   .then((resp) => {
      //  console.log(resp)
        
      //   // this.tableData=resp.data
      //   //  console.log(resp.data)
      //   });


        
     this.$msgbox({
        title: "确认提示",
        message:"是否确定提交？"
        ,
        showCancelButton: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = "执行中...";
           this.correctTitle=this.tableData
     this.correctTitle[0].u_id=this.$store.state.userid
         this.$axios
        .post(
          "http://localhost:8080/correcting_homework",this.correctTitle,
          { headers: { "Content-Type": "application/JSON" }}
          //完成作业ID
        // {params:{list: this.tableData}}
        )
        .then((resp) => {
       console.log(resp.data)
        this.message=resp.data
        // this.tableData=resp.data
        //  console.log(resp.data)
        });

            setTimeout(() => {
              done();
              setTimeout(() => {
                instance.confirmButtonLoading = false;
              }, 300);
            }, 1000);        
          } else {
            done();
          }
        }
      })
      .then(action => {
       if(this.message){
          this.$message({
         message:"批改成功",
        type: "success"
        });
       }
       else{
          this.$message({
         message:"批改失败，请重新批改！",
       
        });
       }
      });

  },
  //批改答案，正确
  startcorrect(row,index){
    console.log(row);
    this.tableData[index].iserror="对"
      //  row.iserror="对"
   },
   //判断作业类型
    questiontype(titleType) {
      return this.type_question[titleType - 1];
    },
     //批改答案，错误
   iserror(row,index){
 this.tableData[index].iserror="错"
   },
  //获取题目与答案
   correct(){
    //  console.log(this.sid)
      this.$axios
        .get(
          "http://localhost:8080/select_homework",
          //完成作业ID
          {params:{sid:this.sid}}
        )
        .then((resp) => {
       console.log(resp.data)
        
        this.tableData=resp.data
  
        });
   }
  },

    mounted(){
      this.correct()
    }
};
</script>
<style scoped></style>
